using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class School : EveProperty
    {
        private int raceId;
        private string schoolName;
        private string description;
        private int graphicId;
        private int corporationId;
        private int agentId;
        private int newAgentId;
        private int tutorialContentId;
        private int careerId;

        private Race race;
        private Graphic graphic;
        private Corporation corporation;
        private Agent agent;
        private Agent newAgent;
        private Career career;
        private Dictionary<int, Agent> agents;

        public Race Race
        {
            get
            {
                if (race == null)
                    race = new Race(db, raceId);
                return race;
            }
        }
        public string name
        {
            get { return schoolName; }
        }
        public string Description
        {
            get { return description; }
        }
        public Graphic Graphic
        {
            get
            {
                if (graphic == null)
                    graphic = new Graphic(db, graphicId);
                return graphic;
            }
        }
        public Corporation Corporation
        {
            get
            {
                if (corporation == null)
                    corporation = new Corporation(db, corporationId);
                return corporation;
            }
        }
        public Agent TutorialAgent
        {
            get
            {
                if (agent == null)
                    agent = new Agent(db, agentId);
                return agent;
            }
        }
        public Agent NewAgent
        {
            get
            {
                if (newAgent == null)
                    newAgent = new Agent(db, newAgentId);
                return newAgent;
            }
        }
        public int TutorialContentId
        {
            get { return tutorialContentId; }
        }
        public Career Career
        {
            get
            {
                if (career == null)
                    career = new Career(db, careerId);
                return career;
            }
        }
        public Dictionary<int, Agent> Agents
        {
            get
            {
                if (agents == null)
                {
                    agents = new Dictionary<int, Agent>();
                    string query = "SELECT chrAgent.*, chrSchoolAgents.agentIndex FROM chrSchoolAgents, chrAgents WHERE chrAgents.agentID=chrSchoolAgents.agentID AND chrSchoolAgents.schoolID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        agents[row["agentIndex"]] = new Agent(row);
                }
                return agents;
            }
        }

        internal School(DbDataRow dr)
            : base(dr.Database, EvePropertyType.School, dr["schoolID"])
        {
            Fill(dr);
        }
        internal School(Database db, int id)
            : base(db, EvePropertyType.School, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM chrSchools WHERE schoolID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            raceId = dr["raceID"];
            schoolName = dr["schoolName"];
            description = dr["description"];
            graphicId = dr["graphicID"];
            corporationId = dr["corporationID"];
            agentId = dr["agentID"];
            newAgentId = dr["newAgentID"];
            tutorialContentId = dr["tutorialContentID"];
            careerId = dr["careerID"];
        }
    }
}
